package com.sense.kukoo.biz_explore.view

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.tabs.TabLayoutMediator
import com.sense.feel.common.utils.ToastUtils
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.biz_explore.R
import com.sense.kukoo.biz_explore.databinding.BizExploreFragmentExploreBinding
import com.sense.kukoo.biz_explore.vm.ExploreViewModel
import com.sense.kukoo.common.mvvm.view.BaseMvvmFragment
import com.sense.kukoo.common.repo.bean.IndexBean
import com.sense.kukoo.common.wiget.adapter.CommonPagerAdapter
import com.sense.kukoo.common.wiget.state.NetworkErrorState
import com.zy.multistatepage.state.SuccessState

class ExploreFragment : BaseMvvmFragment<ExploreViewModel, BizExploreFragmentExploreBinding>() {

    private val TAG: String = javaClass.simpleName

    private lateinit var mFragments:ArrayList<Fragment>
    private lateinit var mAdapter:CommonPagerAdapter

    companion object {
        @JvmStatic
        fun newInstance() = ExploreFragment()
    }

    override fun getLayout(): Int = R.layout.biz_explore_fragment_explore

    override fun getViewModelClass(): Class<ExploreViewModel> = ExploreViewModel::class.java

    override fun initData() {
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        //
        mViewModel.mIndex.observe(viewLifecycleOwner){
            multiState.show<SuccessState>()
            initNav(it)
        }
        //
        mBinding.search.setOnClickListener{
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_EXPLORE_MODULE_SEARCH_ACTIVITY)
                .navigation()
        }
    }

    override fun initView() {}

    override fun singleLazyLoad() {
        //获取导航栏
        mViewModel.getIndex()
    }

    override fun onShowNetworkError() {
        ToastUtils.showToast(getString(com.sense.kukoo.common_i18n.R.string.search_no_net_toast))
        multiState.show<NetworkErrorState> {
            it.onRetry {
                mViewModel.getIndex()
            }
        }
    }

    override fun initParam() {}

    override fun responseVM(action: Int) {}

    fun initNav(index:IndexBean){
        val navMenus: List<IndexBean.NavMenu> = index.navMenus
        mFragments = arrayListOf<Fragment>()
        navMenus.forEach {
            mFragments.add(ContentFragment.newInstance(it))
        }
        mAdapter = CommonPagerAdapter(activity as FragmentActivity, mFragments)
        //
        mBinding.viewPager.adapter = mAdapter
        mBinding.viewPager.isUserInputEnabled = true
        //
        val arrayTabStrings = arrayOfNulls<String>(navMenus.size)
        navMenus.forEachIndexed {i, menu->
            arrayTabStrings.set(i, menu.title)
        }
        mBinding.topTabs.init(arrayTabStrings)
        TabLayoutMediator(mBinding.topTabs, mBinding.viewPager) { tab, position ->
            mBinding.topTabs.setTab(tab, position)
        }.attach()
    }

}